package com.itheima.mall.web.sms;

import com.itheima.mall.commons.result.Result;
import com.itheima.mall.dubbo.api.sms.HomeBrandService;
import com.itheima.mall.entity.domain.sms.HomeBrand;
import com.itheima.mall.entity.domain.vo.smsre.HomeBrandResult;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//首页品牌管理Controller
@ResponseBody
@RestController
@RequestMapping("/home/brand")
@CrossOrigin(origins = "*")
public class HomeBrandController {

    //@Autowired
    @DubboReference
    private HomeBrandService homeBrandService;

    @PostMapping("/create")

    public Result create(@RequestBody List<HomeBrand> homeBrandList) {

        System.out.println("1111111111111111");
        for (HomeBrand homeBrand : homeBrandList) {
            homeBrandService.save(homeBrand);
        }

        return Result.success("成功");
    }

    @PostMapping("/delete")
    @ResponseBody
    public Result delete(@RequestParam List<Long> ids) {
        System.out.println(ids);
        for (Long id : ids) {
            homeBrandService.removeById(id);
        }
        return Result.success("成功");
    }

    @PostMapping("/update/recommendStatus")
    @ResponseBody
    public Result updaterecommendStatus(@RequestParam List<Long> ids, @RequestParam Integer recommendStatus) {
        List<HomeBrand> homeBrandList = homeBrandService.listByIds(ids);
        for (HomeBrand homeBrand : homeBrandList) {
            homeBrand.setRecommendStatus(recommendStatus);
            homeBrandService.updateById(homeBrand);
        }
        return Result.success("成功");

    }


    @RequestMapping("/update/sort/{id}")
    public Result updateSort(@PathVariable Long id, Integer sort) {
        System.out.println(id);
        System.out.println(sort);
        homeBrandService.updateSort(id, sort);
        return Result.success();
    }

    @GetMapping("list")
    public Result list(
            @RequestParam(value = "brandName", required = false) String brandName,
            @RequestParam(value = "recommendStatus", required = false) Integer recommendStatus,
            @RequestParam(value = "pageSize") Integer pageSize,
            @RequestParam(value = "pageNum") Integer pageNum
    ) {
        System.out.println(pageSize);
        System.out.println(pageNum);

        HomeBrandResult brandResult = homeBrandService.page(pageNum, pageSize, recommendStatus, brandName);
        return Result.success(brandResult);
    }

}
